<template>
  <el-dialog :title="this.prodId?'修改':'新增'" :visible.sync="visible">
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm">
      <el-form-item prop="name" label="标签名称" :label-width="formLabelWidth">
        <el-input v-model="ruleForm.name" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="状态" :label-width="formLabelWidth">
        <el-radio-group v-model="ruleForm.radio" v-removeAriaHidden>
          <el-radio label="1">正常</el-radio>
          <el-radio label="0">禁用</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="列表样式" :label-width="formLabelWidth">
        <el-radio-group v-model="ruleForm.radio1" ref="radio" v-removeAriaHidden>
          <el-radio label="0">一列一个</el-radio>
          <el-radio label="1">一列两个</el-radio>
          <el-radio label="2">一列三个</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="排序" :label-width="formLabelWidth">
        <el-input-number
          v-model="ruleForm.num"
          controls-position="right"
          @change="handleChange"
          :min="0"
          :max="100"
        ></el-input-number>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="resetForm()">取 消</el-button>
      <el-button type="primary" @click="submitForm('ruleForm')"
        >确 定</el-button
      >
    </div>
  </el-dialog>
</template>

<script>
import { Debounce } from "@/utils/debounce";
export default {
  data() {
    return {
      visible: false,
      ruleForm: {
        name: "",
        radio: "1",
        radio1: "0",
        num: 0,
      },
      rules: {
        name: [{ required: true, message: "标签不能为空", trigger: "blur" }],
      },
      formLabelWidth: "100px",

      prodId: "",
      dataForm: {},
    };
  },
  methods: {
    init(id) {
      this.prodId = id || 0;
      this.visible = true;
      console.log(id);
      this.$nextTick(() => {
        this.ruleForm.name = "";
        this.ruleForm.radio = "1";
        this.ruleForm.radio1 = "0";
        this.ruleForm.num = 0;
        if (this.prodId) {
          this.$http.get("apis/prod/prodTag/info/" + id).then((res) => {
            console.log(res);

            if (res.code == "00000") {
              this.dataForm = res.data;
              this.ruleForm.name = res.data.title;
              this.ruleForm.radio = res.data.status.toString();
              this.ruleForm.radio1 = res.data.style.toString();
              this.ruleForm.num = res.data.seq;
            }
            console.log(this.dataForm);

            console.log(this.ruleForm.radio);
            console.log(this.ruleForm.radio1);
          });
        }
      });
    },
    handleChange(value) {
      console.log(this.num);
    },
    submitForm: Debounce(function (formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.dataForm.title = this.ruleForm.name;
          this.dataForm.status = Number(this.ruleForm.radio);
          this.dataForm.style = Number(this.ruleForm.radio1);
          this.dataForm.seq = this.ruleForm.num;
          console.log(JSON.parse(JSON.stringify(this.dataForm)));

          this.$http({
            url: "apis/prod/prodTag",
            method: this.prodId ? "put" : "post",
            data: this.dataForm,
          }).then((res) => {
            console.log(res);
            // this.ruleForm.name = "";
            // this.ruleForm.radio = "1";
            // this.ruleForm.radio1 = "0";
            // this.ruleForm.num = 0;
            this.$refs['ruleForm'].resetFields()
            this.$message({
              message: "操作成功",
              type: "success",
              duration: 1500,
              onClose: () => {
                this.visible = false;
                this.$emit("refreshDataList", this.params);
              },
            });
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    }),
    resetForm() {
      this.visible = false;
    },
  },
};
</script>

<style>
</style>